package cn.ibobo.lease.admin.service.impl;

import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.admin.dao.AreaMapper;
import cn.ibobo.lease.admin.dto.AreaDTO;
import cn.ibobo.lease.admin.service.AreaService;
import cn.ibobo.lease.common.util.UuidUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 商户/租赁点区域表业务处理接口
 * @Author lililiang
 * @Date 2018-12-13 18:38:03
 */
@Service
public class AreaServiceImpl extends BaseServiceImpl<AreaDTO, String> implements AreaService {

    private static final Log LOGGER = LogFactory.getLog(AreaServiceImpl.class);

    @Autowired
    private AreaMapper mapper;

    @Override
    protected BaseMapper<AreaDTO, String> getMapper() {
        return mapper;
    }

    /**
     * 商户/租赁点区域表列表
     * @Author lililiang
     * @Date 2018-12-13 18:38:03
     * @Param
     * @Return
     */
    @Override
    public List<AreaDTO> list4Page(AreaDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 商户/租赁点区域表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-13 18:38:03
     * @Param
     * @Return
     */
    @Override
    public List<AreaDTO> listByCondition(AreaDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询商户/租赁点区域表详情信息
     * @Author lililiang
     * @Date 2018-12-13 18:38:03
     * @Param
     * @Return
     */
    @Override
    public AreaDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存商户/租赁点区域表信息
     * @Author lililiang
     * @Date 2018-12-13 18:38:03
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(AreaDTO entity) {
        String id = entity.getId();
        if(id == null || id.length() == 0){
            entity.setId(UuidUtil.getUuid());
            return mapper.add(entity);
        }else{
            return mapper.edit(entity);
        }
    }

    /**
     * 删除商户/租赁点区域表信息
     * @Author lililiang
     * @Date 2018-12-13 18:38:03
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }

    /**
     * @Description 查询区域树
     * @Author lililiang
     * @Date 2018/12/14 20:52
     * @Param [entityDTO]
     * @Return java.util.List<cn.ibobo.lease.admin.dto.AreaDTO>
     */
    @Override
    public List<AreaDTO> getAreaTree(AreaDTO entityDTO){
        List<AreaDTO> areaDTOS = mapper.listByCondition(entityDTO);
        List<AreaDTO> topArea = new ArrayList<>();
        if(areaDTOS != null && areaDTOS.size() > 0){
            for(AreaDTO tempDTO : areaDTOS){
                if("0".equals(tempDTO.getPid())){
                    topArea.add(tempDTO);
                }
            }
            if(topArea != null && topArea.size() > 0){
                this.recursionArea(topArea, areaDTOS);
            }
        }
        return topArea;
    }

    /**
     * @Description 递归处理区域信息
     * @Author lililiang
     * @Date 2018/12/14 20:59
     * @Param [topArea, areaDTOS]
     * @Return void
     */
    private void recursionArea(List<AreaDTO> topArea, List<AreaDTO> areaDTOS) {
        for(AreaDTO tempTop : topArea){
            List<AreaDTO> children = new ArrayList<>();
            for(AreaDTO tempSub : areaDTOS){
                if(tempTop.getId().equals(tempSub.getPid())){
                    children.add(tempSub);
                }
            }
            tempTop.setChildren(children);
            if(children != null && children.size() > 0){
                this.recursionArea(children, areaDTOS);
            }
        }
    }
}
